AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include('shared.lua')

function ENT:Initialize()
	self.BaseClass.Initialize(self)

	self.Shield = ents.Create("prop_dynamic_override")
	self.Shield:SetModel("models/Items/car_battery01.mdl")
	self.Shield:SetPos(self:GetPos()+self:GetAngles():Up()*-18 + Vector(0, -10, 0))
	self.Shield:SetAngles(Angle(0, 0, 0))
	self.Shield:SetParent(self)
	self.Shield:SetSolid(SOLID_NONE)
	self.Shield:SetMoveType(MOVETYPE_NONE)

	self.Shield2 = ents.Create("prop_dynamic_override")
	self.Shield2:SetModel("models/Items/car_battery01.mdl")
	self.Shield2:SetPos(self:GetPos()+self:GetAngles():Up()*-18 + Vector(0, 10, 0))
	self.Shield2:SetAngles(Angle(0, 0, 0))
	self.Shield2:SetParent(self)
	self.Shield2:SetSolid(SOLID_NONE)
	self.Shield2:SetMoveType(MOVETYPE_NONE)

	self.Hat = ents.Create("prop_dynamic_override")
	self.Hat:SetModel("models/props/de_tides/vending_hat.mdl")
	self.Hat:SetPos(self:GetPos()+self:GetAngles():Up()*11 + Vector(0, -10, 0))
	self.Hat:SetAngles(Angle(0, 0, 0))
	self.Hat:SetParent(self)
	self.Hat:SetSolid(SOLID_NONE)
	self.Hat:SetMoveType(MOVETYPE_NONE)

	self.Hat1 = ents.Create("prop_dynamic_override")
	self.Hat1:SetModel("models/props/de_tides/vending_hat.mdl")
	self.Hat1:SetPos(self:GetPos()+self:GetAngles():Up()*11 + Vector(0, 10, 0))
	self.Hat1:SetAngles(Angle(0, 0, 0))
	self.Hat1:SetParent(self)
	self.Hat1:SetSolid(SOLID_NONE)
	self.Hat1:SetMoveType(MOVETYPE_NONE)
		
	self.Scanner = ents.Create("prop_dynamic_override")
	self.Scanner:SetModel("models/props_lab/monitor01b.mdl")
	self.Scanner:SetPos(self:GetPos()+self:GetAngles():Up()*-1 + Vector(0, -10, 0))
	self.Scanner:SetAngles(self:GetAngles()+Angle(0, 0, 0))
	self.Scanner:SetParent(self)
	self.Scanner:SetSolid(SOLID_NONE)
	self.Scanner:SetMoveType(MOVETYPE_NONE)
	self.Scanner:SetNoDraw(true)

	self.Scanner2 = ents.Create("prop_dynamic_override")
	self.Scanner2:SetModel("models/props_lab/monitor01b.mdl")
	self.Scanner2:SetPos(self:GetPos()+self:GetAngles():Up()*-1 + Vector(0, 10, 0))
	self.Scanner2:SetAngles(self:GetAngles()+Angle(0, 0, 0))
	self.Scanner2:SetParent(self)
	self.Scanner2:SetSolid(SOLID_NONE)
	self.Scanner2:SetMoveType(MOVETYPE_NONE)
	self.Scanner2:SetNoDraw(true)

	self.Crowbar = ents.Create("prop_dynamic_override")
	self.Crowbar:SetModel("models/weapons/w_crowbar.mdl")
	self.Crowbar:SetPos(self:GetPos()+self:GetAngles():Up()*-38 + Vector(5, 0, 0))
	self.Crowbar:SetAngles(self:GetAngles()+Angle(0, 90, 0))
	self.Crowbar:SetParent(self)
	self.Crowbar:SetSolid(SOLID_NONE)
	self.Crowbar:SetMoveType(MOVETYPE_NONE)
	self.Crowbar:SetNoDraw(true)

	self.Crowbar2 = ents.Create("prop_dynamic_override")
	self.Crowbar2:SetModel("models/weapons/w_crowbar.mdl")
	self.Crowbar2:SetPos(self:GetPos()+self:GetAngles():Up()*-38 + Vector(-5, 0, 0))
	self.Crowbar2:SetAngles(self:GetAngles()+Angle(0, 90, 0))
	self.Crowbar2:SetParent(self)
	self.Crowbar2:SetSolid(SOLID_NONE)
	self.Crowbar2:SetMoveType(MOVETYPE_NONE)
	self.Crowbar2:SetNoDraw(true)	

	self.Toolkit = ents.Create("prop_dynamic_override")
	self.Toolkit:SetModel("models/props_c17/tools_pliers01a.mdl")
	self.Toolkit:SetPos(self:GetPos()+self:GetAngles():Up()*33 + Vector(-7, 10, 0))
	self.Toolkit:SetAngles(self:GetAngles()+Angle(90, 90, 90))
	self.Toolkit:SetParent(self)
	self.Toolkit:SetSolid(SOLID_NONE)
	self.Toolkit:SetMoveType(MOVETYPE_NONE)
	self.Toolkit:SetNoDraw(true)

	self.Toolkit2 = ents.Create("prop_dynamic_override")
	self.Toolkit2:SetModel("models/props_c17/tools_pliers01a.mdl")
	self.Toolkit2:SetPos(self:GetPos()+self:GetAngles():Up()*33 + Vector(-7, -10, 0))
	self.Toolkit2:SetAngles(self:GetAngles()+Angle(90, 90, 90))
	self.Toolkit2:SetParent(self)
	self.Toolkit2:SetSolid(SOLID_NONE)
	self.Toolkit2:SetMoveType(MOVETYPE_NONE)
	self.Toolkit2:SetNoDraw(true)
	
	self.Binoculars = ents.Create("prop_dynamic_override")
	self.Binoculars:SetModel("models/weapons/w_binoculars.mdl")
	self.Binoculars:SetPos(self:GetPos()+self:GetAngles():Up()*-35 + Vector(2, 5, 0))
	self.Binoculars:SetAngles(self:GetAngles()+Angle(90, 90, 0))
	self.Binoculars:SetParent(self)
	self.Binoculars:SetSolid(SOLID_NONE)
	self.Binoculars:SetMoveType(MOVETYPE_NONE)
	self.Binoculars:SetNoDraw(true)

	self.Binoculars2 = ents.Create("prop_dynamic_override")
	self.Binoculars2:SetModel("models/weapons/w_binoculars.mdl")
	self.Binoculars2:SetPos(self:GetPos()+self:GetAngles():Up()*-35 + Vector(2, -5, 0))
	self.Binoculars2:SetAngles(self:GetAngles()+Angle(90,90,0))
	self.Binoculars2:SetParent(self)
	self.Binoculars2:SetSolid(SOLID_NONE)
	self.Binoculars2:SetMoveType(MOVETYPE_NONE)
	self.Binoculars2:SetNoDraw(true)
end

function ENT:UpgradeCallback(level)	
	if(level == 1) then
		self.Toolkit:SetNoDraw(false)
		self.Toolkit2:SetNoDraw(false)
		self.Scanner:SetNoDraw(false)
		self.Scanner2:SetNoDraw(false)
	end
	if(level ==2) then
		self.Crowbar:SetNoDraw(false)
		self.Crowbar2:SetNoDraw(false)
		self.Binoculars:SetNoDraw(false)
		self.Binoculars2:SetNoDraw(false)
	end
end

function ENT:Use(ply, caller)
	if (!ply:CanAfford(self.SupplyCost)) then
		Notify(ply, 4, 3, "Cannot afford to buy supplies!")
		return 
	end
	
	if !self.dt.Sparking then
		if !ply:GetNWBool("Snipe Shieldd") then
			ply.Shielded = true
			ply:SetNWBool("Snipe Shieldd", true)
			self.dt.Sparking = true
		end
		if !ply:GetNWBool("Helmetd") then
			ply:SetNWBool("Helmetd", true)
			self.dt.Sparking = true
		end
		
		if self.Level >= 1 then
			if !ply:GetNWBool("Scannerd") then
				ply:SetNWBool("Scannerd", true)
				self.dt.Sparking = true
			end
			if !ply:GetNWBool("Toolkitd") then
				ply.Tooled = true
				ply:SetNWBool("Toolkitd", true)
				self.dt.Sparking = true
			end
		end
		
		if self.Level == 2 && (!IsValid(ply:GetWeapon("lockpick")) || !IsValid(ply:GetWeapon("binoculars"))) then
			ply:Give("lockpick")
			ply:Give("binoculars")
			self.dt.Sparking = true
		end
		
		if(self.dt.Sparking) then 
			if(self.Surplus > 1)then
				self.Surplus = self.Surplus - 1
				Notify(ply, 2, 3, string.format("Free supplies due to surplus of %i.", self.Surplus))
			else
				ply:TakeMoney(self.SupplyCost)
				Notify(ply, 2, 3, string.format("Charged $%i for buying supplies.", self.SupplyCost))
			end
		else
			return
		end
		timer.Create(tostring(self) .. "resupply", self.SupplyDelay, 1, self.SetSparking, self, false)
	end
end

function ENT:StartTouch(ent)
	if(ent.Base == "base_item" && self.Surplus < 20) then
		self.Surplus = self.Surplus + 1
		ent:Remove()
	end
end

function ENT:OnRemove()
	timer.Remove(tostring(self) .. "resupply")
end

